package com.example.bysj_sb.dao;

import com.example.bysj_sb.domain.GraduateProjectTrueness;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("graduateProjectTruenessDao")
public class GraduateProjectTruenessDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //增加
    public boolean add(GraduateProjectTrueness graduateProjectTrueness) {
        String sql = "INSERT INTO GraduateProjectTrueness (no,description,remarks) VALUES (?,?,?)";
        int affectedRowNum = jdbcTemplate.update(sql, graduateProjectTrueness.getNo(), graduateProjectTrueness.getDescription(),graduateProjectTrueness.getRemarks());
        if (affectedRowNum != 1) {
            return false;
        } else {

            return true;
        }
    }

    public GraduateProjectTrueness find(int id){
        String sql = "SELECT * FROM GraduateProjectTrueness WHERE id = ?";
        List<GraduateProjectTrueness> graduateProjectTruenesses = jdbcTemplate.query(sql, new Object[]{id},
                new BeanPropertyRowMapper<>(GraduateProjectTrueness.class));
        if(!graduateProjectTruenesses.isEmpty()){
            return graduateProjectTruenesses.get(0);
        } else
            return null;
    }
    public List<GraduateProjectTrueness> findAll(){
        String sql = "SELECT * FROM GraduateProjectTrueness";

        List<GraduateProjectTrueness> graduateProjectTruenesses =
                jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(GraduateProjectTrueness.class){

        });
        return graduateProjectTruenesses;
    }
    public boolean update(GraduateProjectTrueness graduateProjectTrueness){
        String sql = "UPDATE GraduateProjectTrueness SET no = ? ,description=? WHERE id = ?";
        int affectedRowNum = jdbcTemplate.update(sql,graduateProjectTrueness.getNo(),graduateProjectTrueness.getDescription(),graduateProjectTrueness.getId());
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
    public boolean delete(int id){
        String sql = "DELETE FROM GraduateProjectTrueness WHERE id = ? ";
        int affectedRowNum = jdbcTemplate.update(sql,id);
        if (affectedRowNum!=1){
            return false;
        }else{
            return true;
        }
    }
}